home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- from __future__ import with_statement
- from traceback import print_exc
- from util import Storage as S, tag, plisttype_to_pytype, odict
- from collections import defaultdict
- from PIL import Image
- from os.path import getmtime
- import re
- from logging import getLogger
- log = getLogger('emoticons')
- emoticon_loaders = [
- 'digsby',
- 'pidgin',
- 'adium']
-
- def is_emoticon_dir(d):
- G = globals()
- for loader in emoticon_loaders:
- if G['is_emoticons_' + loader](d):
- return True
- continue
-
-
-
- def is_emoticons_digsby(emoticon_dir):
- emoticons_txt = emoticon_dir / 'emoticons.txt'
- return emoticons_txt.isfile()
-
-
- def load_emoticons_digsby(emoticon_dir):
- emoticons_txt = emoticon_dir / 'emoticons.txt'
- if not emoticons_txt.isfile():
- return (None, None)
-
- title = emoticon_dir.name
- emoticons = { }
- bitmaps = odict()
-
- try:
- f = _[2]
- for line in f:
- line = line.strip()
- if line:
- title = line
- break
- continue
- file(emoticons_txt)
-
- for line in f:
- line = line.strip()
- if not line:
- continue
-
- content = line.split()
- image_filename = content[0]
- smileys = content[1:]
- imgpath = emoticon_dir / image_filename
- if imgpath.exists():
- for smiley in smileys:
- emoticons[smiley.encode('xml')] = S(path = imgpath)
- if imgpath not in bitmaps:
- bitmaps[imgpath] = [
- smiley]
- continue
- bitmaps[imgpath].append(smiley)
-
- finally:
- pass
-
- return (emoticons, bitmaps)
-
-
- def is_emoticons_pidgin(emoticon_dir):
- return (emoticon_dir / 'theme').isfile()
-
-
- def load_emoticons_pidgin(emoticon_dir):
- theme = emoticon_dir / 'theme'
- if not theme.isfile():
- return (None, None)
-
- smileys = defaultdict(list)
- emoticons = { }
- bitmaps = odict()
-
- try:
- f = _[2]
- for line in f:
- if line.count('\t') > 0:
- seq = filter(None, line.strip().split('\t'))
- if len(seq) >= 2:
- (img, smiley) = seq[:2]
- imgpath = emoticon_dir / img
- if imgpath.exists():
- emoticons[smiley.encode('xml')] = S(path = imgpath)
- if imgpath not in bitmaps:
- bitmaps[imgpath] = [
- smiley]
- else:
- bitmaps[imgpath].append(smiley)
-
-
- len(seq) >= 2
- finally:
- pass
-
- return (emoticons, bitmaps)
-
-
- def is_emoticons_adium(emoticon_dir):
- return (emoticon_dir / 'Emoticons.plist').isfile()
-
-
- def load_emoticons_adium(emoticon_dir):
- emoticons_txt = emoticon_dir / 'Emoticons.plist'
- if not emoticons_txt.isfile():
- return (None, None)
-
-
- try:
- f = _[2]
- plist = tag(f.read())
- toplevel = plist._children[0]
- converted = plisttype_to_pytype(toplevel)
- emoticons = { }
- bitmaps = odict()
- for img_name, info in converted['Emoticons'].items():
- smileys = info['Equivalents']
- imgpath = emoticon_dir / img_name
- if imgpath.exists():
- for smiley in smileys:
- if not smiley:
- continue
-
- emoticons[smiley.encode('xml')] = S(path = imgpath)
- if imgpath not in bitmaps:
- bitmaps[imgpath] = [
- smiley]
- continue
- bitmaps[imgpath].append(smiley)
-
- finally:
- pass
-
- return (emoticons, bitmaps)
-
-
- def get_emoticon_dirs():
- dirs = [
- resdir() / 'emoticons']
-
- try:
- import stdpaths
- userdir = stdpaths.userdata / 'Emoticon Sets'
- if not userdir.isdir():
- userdir.makedirs()
- except Exception:
- print_exc()
-
- dirs.append(userdir)
- return dirs
-
-
- def load_emoticons(emoticon_pack = 'default'):
- log.info('load_emoticons: %s', emoticon_pack)
- emoticons = None
- for emoticon_dir in get_emoticon_dirs():
- emoticon_packdir = emoticon_dir / emoticon_pack
- for loader in emoticon_loaders:
-
- try:
- (emoticons, bitmaps) = globals()['load_emoticons_' + loader](emoticon_packdir)
- except Exception:
- print_exc()
-
- if emoticons:
- break
- continue
-
- if emoticons:
- break
- continue
-
- if not emoticons:
- return (emoticons, { }, [])
-
- emotitems = sorted(emoticons.items(), key = (lambda e: len(e[0])), reverse = True)
- smiley_regex = '|'.join((lambda .0: for smiley, emoticon in .0:
- '(?:%s)' % re.escape(smiley))(emotitems))
- patterns = [
- '(?:^|\\s)(%s)(?:\\s|$)']
- compiled_patterns = [ re.compile(r % smiley_regex, re.MULTILINE) for r in patterns ]
- return (emoticons, bitmaps, compiled_patterns)
-
- _emoticons = sentinel
- _emoticons_pack = sentinel
- _bitmaps = None
-
- def first_non_none(seq):
- for i, elem in enumerate(seq):
- if elem is not None:
- return i
- continue
-
- raise AssertionError
-
-
- def repl(emoticons):
-
- def _repl(m, e = emoticons):
- (x, y) = m.span()
- (i, j) = m.span(1)
- s = m.string
- emot = e[s[i:j]]
- size = imgsize(emot.path)
- emottext = s[i:j].encode('xml')
- replacement = '<img src="%s" width="%d" height="%d" alt="%s" title="%s" />' % (emot.path.url(), size[0], size[1], emottext, emottext)
- return ''.join([
- s[x:i],
- replacement,
- s[j:y]])
-
- return _repl
-
-
- def apply_emoticons(s, emoticon_pack = 'default'):
- global _emoticons, _bitmaps, _emoticon_patterns, _emoticons_pack, _bitmaps
- if _emoticons is sentinel or emoticon_pack != _emoticons_pack:
- (_emoticons, _bitmaps, _emoticon_patterns) = load_emoticons(emoticon_pack)
- _emoticons_pack = emoticon_pack
- _bitmaps = list(_bitmaps.items())
-
- for p in _emoticon_patterns:
- s = p.sub(repl(_emoticons), s)
- s = p.sub(repl(_emoticons), s)
-
- return s
-
-
- def findsets():
- sets = []
- for d in get_emoticon_dirs():
- for subdir in d.dirs():
- if is_emoticon_dir(subdir):
- sets.append(subdir)
- continue
-
-
- return sets
-
-
- def imgsize(p, _cache = { }):
- key = (p, getmtime(p))
-
- try:
- return _cache[key]
- except KeyError:
- size = Image.open(p).size
- return _cache.setdefault(key, size)
-
-
- if __name__ == '__main__':
- from path import path
-
- def resdir():
- return path('../../../res')
-
- else:
-
- def resdir():
- skin = skin
- import gui
- return skin.resourcedir()
-
- if __name__ == '__main__':
- print load_emoticons_pidgin(path('../../../res/emoticons/sa'))
-
-